from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import datetime

# Create your models here.

def user_file_path(instance, filename):
    dt = datetime.now()
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    return '{1}/file/{0}'.format(filename,dt.strftime('%Y%m%d'))
def user_image_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    dt = datetime.now()
    return '{1}/image/{0}'.format(filename,dt.strftime('%Y%m%d'))

class Task(models.Model):
    name = models.CharField('名称',max_length=20)
    point = models.PositiveIntegerField('点数')
    created_at = models.DateTimeField('建立时间',auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(User, related_name='create_tasks',on_delete='CASCADE')
    maxdealer = models.PositiveIntegerField('最大数量',default=1)
    divide_type_choices = {
        ('0','平分'),
        ('1','定额'),
        ('2','自定义'),
    }
    divide_type = models.CharField('分配类型',choices=divide_type_choices,max_length=1) # 0 平分 1 自定义
    task_status_choices= {
         ('0','可用'),
         ('1','已领完'),
         ('2','完成'),
         ('3','取消'),
    }
    status = models.CharField('状态',choices=task_status_choices,max_length=1,default='0') # 0 可用 1 全部领取 2 完成 3 取消
    task_typ_choices= {
         ('0','工作任务'),
         ('2','个人任务'),
         ('1','礼物'),
    }
    task_typ = models.CharField('类型',choices=task_typ_choices,max_length=1,default='0') # 0 任务 1 礼物
    exp_date = models.DateTimeField('有效期至',null=True)
    descrition = models.TextField('描述',default='这里没有任何有用的信息')
    picture = models.ImageField('图片',upload_to=user_image_path,null=True)
    attachment = models.FileField('附件',upload_to=user_file_path,null=True)

class TaskDealer(models.Model):
    task = models.ForeignKey(Task,related_name='dealer',on_delete='CASCADE')
    dealer = models.ForeignKey(User,related_name='my_tasks',on_delete='CASCADE')
    point  = models.IntegerField()
    dealer_status_choices= {
         ('1','领取'),
         ('2','完成'),
         ('3','取消'),
         ('4','放弃'),
    }
    status = models.CharField('处理状态',choices=dealer_status_choices,max_length=1,default='1') # 1 领取 2 完成 3 取消 4放弃
    got_at = models.DateTimeField(auto_now_add=True)
    done_at = models.DateTimeField(null=True)



class Account(models.Model):
    lastbal = models.IntegerField(default=0)
    currbal = models.IntegerField(default=0)
    totalbal = models.IntegerField(default=0)
    own_by = models.ForeignKey(User,related_name = 'my_account',on_delete='CASCADE')


class Translog(models.Model):
    to_account = models.ForeignKey(Account,related_name='from_account',on_delete='CASCADE') 
    from_account = models.ForeignKey(Account,related_name='to_account',on_delete='CASCADE') 
    trandate = models.DateTimeField(auto_now_add=True)
    tranamt = models.IntegerField() 
    by_dealer = models.ForeignKey(TaskDealer,related_name = 'translog',on_delete='CASCADE')
    on_task = models.ForeignKey(Task, related_name = 'translog', on_delete='CASCADE')
